package com.senney.practice.nio.test;

import org.junit.Test;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.CharacterCodingException;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CharsetEncoder;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.Map;
import java.util.Set;

public class ChannelDemo {
    @Test
    public void copyPicByChannel() {
        FileInputStream fis = null;
        FileOutputStream fos = null;
        FileChannel inChannel = null;
        FileChannel outChannel = null;
        try {
            fis = new FileInputStream("C:\\Users\\Lanme\\Pictures\\FB.jpg");
            fos = new FileOutputStream("C:\\Users\\Lanme\\Pictures\\abc.jpg");
            inChannel = fis.getChannel();
            outChannel = fos.getChannel();
            ByteBuffer buffer = ByteBuffer.allocate(1024);
            while (inChannel.read(buffer) != -1) {
                buffer.flip();
                outChannel.write(buffer);
                buffer.clear();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (outChannel != null) {
                try {
                    outChannel.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (inChannel != null) {
                try {
                    inChannel.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @Test
    public void copPicByDirectBuffer() throws IOException {
        FileChannel inChannel = FileChannel.open(Paths.get("D:/", "1.png"), StandardOpenOption.READ);
        FileChannel outChannel = FileChannel.open(Paths.get("D:/", "2.png"), StandardOpenOption.READ, StandardOpenOption.WRITE, StandardOpenOption.CREATE);
        // 内存映射文件
        MappedByteBuffer inMappedBuf = inChannel.map(FileChannel.MapMode.READ_ONLY, 0, inChannel.size());
        MappedByteBuffer outMappedBuf = outChannel.map(FileChannel.MapMode.READ_WRITE, 0, inChannel.size());

        byte[] dst = new byte[inMappedBuf.limit()];
        inMappedBuf.get(dst);
        outMappedBuf.put(dst);

        inChannel.close();
        outChannel.close();
    }

    @Test
    public void transfer() throws IOException {
        FileChannel inChannel = FileChannel.open(Paths.get("D:/", "1.png"), StandardOpenOption.READ);
        FileChannel outChannel = FileChannel.open(Paths.get("D:/", "2.png"), StandardOpenOption.READ, StandardOpenOption.WRITE, StandardOpenOption.CREATE);
        //inChannel.transferTo(0,inChannel.size(),outChannel);
        outChannel.transferFrom(inChannel, 0, inChannel.size());
        inChannel.close();
        outChannel.close();
    }

    @Test
    public void testCharset() throws CharacterCodingException {
//        Map<String, Charset> map = Charset.availableCharsets();
//        for (Map.Entry<String, Charset> entry : map.entrySet()) {
//            System.out.println(entry.getKey() + "=" + entry.getValue());
//        }

        Charset cs1 = Charset.forName("UTF-8");
        CharsetEncoder encoder = cs1.newEncoder();
        CharsetDecoder decoder = cs1.newDecoder();
        CharBuffer buffer = CharBuffer.allocate(1024);
        buffer.put("世界，你好！");
        buffer.flip();

        ByteBuffer byteBuffer = encoder.encode(buffer);
        CharBuffer charBuffer = decoder.decode(byteBuffer);

        System.out.println(charBuffer.toString());
    }
}
